#include<bits/stdc++.h>
using namespace std;
namespace IO{
    char buff[1<<21],*p1=buff,*p2=buff;
    char getch(){
        return p1==p2&&(p2=((p1=buff)+fread(buff,1,1<<21,stdin)),p1==p2)?EOF:*p1++;
    }
    template<typename T>
    void read(T &x){
        char ch=getch();int fl=1;x=0;
        while(ch>'9'||ch<'0'){if(ch=='-')fl=-1;ch=getch();}
        while(ch<='9'&&ch>='0'){x=x*10+ch-48;ch=getch();}
        x*=fl;
    }
    template<typename T,typename ...Args>
    void read(T &x,Args& ...args){
        read(x);read(args...);
    }
    char obuf[1<<21],*p3=obuf;
    void putch(char ch){
        if(p3-obuf<(1<<21))*p3++=ch;
        else fwrite(obuf,p3-obuf,1,stdout),p3=obuf,*p3++=ch;
    }
    void pc(const char *s){
    	while(*s)putch(*s),s++;
    }
    char ch[100];
    template<typename T>
    void write(T x){
        if(!x)return putch('0');
        if(x<0)putch('-'),x*=-1;
        int top=0;
        while(x)ch[++top]=x%10+48,x/=10;
        while(top)putch(ch[top]),top--;
    }
    template<typename T,typename ...Args>
    void write(T x,Args ...args){
        write(x),putch(' '),write(args...);
    }
    void flush(){fwrite(obuf,p3-obuf,1,stdout);}
}
using namespace IO;
const int N=10;
int n,m,len,ans=100;
int b[N][N],B[N][N],fa[N];
vector<pair<int,int>>e;
inline int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
void sol(){
	ans=100,e.clear();
	memset(b,0,sizeof b);
	read(n,m);
	for(int i=1;i<=m;i++){
		int x,y;
		read(x,y);
		x++,y++;
		b[x][y]=b[y][x]=1;
	}
	for(int i=1;i<=n;i++)
		for(int j=1;j<i;j++)
			e.push_back({i,j});
	len=e.size();
	for(int i=0;i<(1<<len);i++){
		for(int j=1;j<=n;j++)fa[j]=j;
		memset(B,0,sizeof B);
		int siz=0;
		for(int j=0;j<len;j++){
			if((i>>j)&1){
				siz++;
				int x=find(e[j].first),y=find(e[j].second);
				fa[x]=y;
			}
		}
		for(int j=1;j<=n;j++){
			for(int k=1;k<=n;k++){
				if(!b[j][k])continue;
				int x=find(j),y=find(k);
				B[x][y]=B[y][x]=1;
			}
		}
		int s=0;
		for(int j=1;j<=n;j++)if(fa[j]==j)s++;
		cout<<s<<'\n';
		for(int j=1;j<=n;j++){
			for(int k=1;k<=n;k++){
				cout<<B[j][k]<<' ';
			}cout<<'\n';
		}cout<<'\n';
		for(int j=1;j<=n;j++)
			for(int k=1;k<=j;k++)
				s-=B[j][k];
		if(s==0)ans=min(ans,siz);
	}
	write(ans),putch('\n');
}
signed main(){
	int T;
	read(T);
	while(T--)sol(); 
	flush();
	return 0;
}/*
1
3 4
0 0
1 2
2 0
0 1

*/

